<?php
	require(dirname(__FILE__) . '/includes/init.php');
    //require(dirname(__FILE__) . '/includes/init.php');
$error = null;
$dao_str = null;
//$dao_success = null;
if($_REQUEST['act'] == 'search'){
	if(!empty($_REQUEST['mobile'])){
		$order_list = order_list(trim($_REQUEST['mobile']),0);
	}else if(!empty($_REQUEST['consignee'])){
		$order_list = order_list(0,trim($_REQUEST['consignee']));
	}
}
/********************************/
elseif ($_REQUEST['act'] == 'read'){
	$sql = "select insert_time from ".$ecs->table('orders')." where order_id = (select max(order_id) from ".$ecs->table('orders').")";
    $inert_time = $db->getOne($sql);
	if(gmtime()-$inert_time<=24*3600){
		$dao_str = "今天您已经导入过数据了";
	}else{
	require_once('includes/cls_readexcel.php'); 
	$data= new Spreadsheet_Excel_Reader(); 
	$data->setOutputEncoding('utf-8'); 
		if ($_FILES["file"]["error"] > 0)
		{
			$error = urlencode('文件出错了，错误码：'.$_FILES["file"]["error"]);
			header("Location: orders.php?error=$error\n");exit;
		}else if(end(explode(".",strtolower($_FILES["file"]['name'])))!='xls'){
			$error = urlencode('请选择excel文件');
			header("Location: orders.php?error=$error\n");exit;
		}
		else
		{
			$datapath = $_FILES["file"]["tmp_name"];
		}
		$data->read($datapath);
        //echo $data->sheets[0]['cells'][3][1].'$$$$$$';exit;
		$count = 0;
		for($i= 2; $i<= $data->sheets[0]['numRows']; $i++){
			//if(strpos($data->sheets[0]['cells'][$i][12],','){
				
			//}
			$str_12 = $data->sheets[0]['cells'][$i][12];	
			//$str_12 = str_replace("y",",",$str_12);
			//$str_12 = preg_replace('/([\x80-\xff]*)|[(\xc2\xa0)|\s]+/i','',$str_12);
			$express_sns = explode('y',$str_12);
			//print_r($express_sns);exit;
			$sn_num = count($express_sns);
			
			for($j=0;$j<$sn_num;$j++){
				$sql = "INSERT INTO " .$ecs->table('orders'). "(order_sn,sys_order_sn,user,money,product_name,mobile,address,fapiao,order_time,express_com,danhao,insert_time,from_where)".
                "VALUES ('".$data->sheets[0]['cells'][$i][2]."', '".$data->sheets[0]['cells'][$i][3]."','".$data->sheets[0]['cells'][$i][4]."',
				'".$data->sheets[0]['cells'][$i][5]."','".$data->sheets[0]['cells'][$i][6]."','".substr($data->sheets[0]['cells'][$i][7],0,11)."','".$data->sheets[0]['cells'][$i][8]."','".$data->sheets[0]['cells'][$i][9]."','".$data->sheets[0]['cells'][$i][10]."','".$data->sheets[0]['cells'][$i][11]."','".trim($express_sns[$j])."','".gmtime()."','".$data->sheets[0]['cells'][$i][1]."')";
			    $db->query($sql);
			}
			$count = $count + $sn_num;
		}
		$dao_str = '已成功导入'.$count.'条';
		//$order_list = order_list();
	}
}
/********************************/
else{
	//$order_list = order_list();
}
function order_list($key='',$key1=''){
	 /* 查询 */
	 if(empty($key)&&empty($key1)){
		$sql = "SELECT o.*,e.enrol_name FROM " .$GLOBALS['ecs']->table('orders')." as o left join ".$GLOBALS['ecs']->table('enrols')."as e on o.order_id=e.order_id";
	 }else if($key){
        $sql = "SELECT o.*,e.enrol_name FROM " .$GLOBALS['ecs']->table('orders')." as o left join ".$GLOBALS['ecs']->table('enrols')."as e on o.order_id=e.order_id where mobile like '%".$key."%'";
	 }else{
		$sql = "SELECT o.*,e.enrol_name FROM " .$GLOBALS['ecs']->table('orders')." as o left join ".$GLOBALS['ecs']->table('enrols')."as e on o.order_id=e.order_id where user like '%".$key1."%'";
	 }
	    $row = $GLOBALS['db']->getAll($sql);
		return $row;
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 订单列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
 
<h1>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 订单列表 </span>
<div style="clear:both"></div>
</h1>
<?php if(!empty($_GET['error'])){?>
<div class="form-div" id="error">
  <span><a href="#" onclick="javascript:err_hide()" title="关闭"><img src="images/no.gif" border="0" alt="关闭"></a><?php echo urldecode($_GET['error']); ?></span>
</div>
<?php }?>
<div class="form-div">
  <form method="post" action="orders.php?act=read" enctype="multipart/form-data">
   Excel<input type="file" name="file" size="50" />
   <input type="submit" name="readexcel" value="导入" class="button"/>
   <?php if($dao_str){?><span style="color:red;"><?php echo $dao_str;?></span><?php }?>
  </form>
</div>

<div class="form-div">
  <form action="orders.php" method="post" name="searchForm" onsubmit="return search_yz()">
    <img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
    手机<input name="mobile" type="text" id="mobile" size="15">
    收货人<input name="consignee" type="text" id="consignee" size="15">
	<input type="hidden" name="act" value="search" />
    <input type="submit" value=" 搜索 " class="button" />
  </form>
</div>
<script type="text/javascript">
function search_yz(){
	var mobile = $("#mobile").val();
	var consignee = $("#consignee").val();
	if(mobile==""&&consignee==""){
		alert("请输入搜索条件");
		return false;
	}else{
		return true;
	}
}
</script> 
<!-- 订单列表 -->
<form method="post" action="order.php?act=operate" name="listForm" onsubmit="return check()">
  <div class="list-div" id="listDiv">
 
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>
      订单来源</th>
    <th>系统订单号</th>
    <th>收货人</th>
    <th>总金额</th>
    <th>产品</th>
    <th>手机</th>
	<th>地址</th>
	<th>订单时间</th>
	<th>快递公司</th>
	<th>单号</th>
	<th>登记人</th>
    <th>操作</th>
  <tr>
  <?php 
    $i=0;
	if(count($order_list)>0){
	foreach($order_list AS $index => $array_val){
		$i++;
  ?>
   <tr>
    <td><?php echo $array_val['from_where']?></td>
    <td><?php echo $array_val['sys_order_sn']?></td>
    <td><?php echo $array_val['user']?></td>
    <td><?php echo $array_val['money']?></td>
    <td><?php echo $array_val['product_name']?></td>
    <td><?php echo $array_val['mobile']?></td>
	<td><span id="order_<?php echo $i;?>"><?php echo sub_str($array_val['address'],10);?></span><div id="address_<?php echo $i;?>" style="height:20px;margin-top:-1px;position:absolute;color:#fff;background-color:red;display:none"><?php echo $array_val['address'];?></div></td>
	<td><?php 
	if(preg_match("/[\x7f-\xff]/", $array_val['order_time'])) {
		echo $array_val['order_time'];
	}else{
		$n = intval(($array_val['order_time']-25569)*3600*24); //转换成1970年以来的秒数
		echo gmdate('Y-m-d', $n);//格式化时间
	}?>
	</td>

	<td><?php echo $array_val['express_com']?></td>
	<td><a href="express.php?com=<?php echo $array_val['express_com']?>&nu=<?php echo $array_val['danhao'];?>" target="_blank"><?php echo $array_val['danhao']?></a></td>
	<td><?php echo  $array_val['enrol_name'];?></td>
    <td><?php if(!$array_val['enrol_name']){ ?><a href="enrol.php?sn=<?php echo  $array_val['order_id'];?>">登记</a><?php }else{ echo '已登记';} ?></td>
  <tr>
  <?php 
      } 
	}
  ?>
  </table>
 
<!-- 分页 -->
  </div>
 
</form>
<script type="text/javascript">
var num = <?php echo count($order_list);?>;
for (i=1;i<=num;i++){
    hide_show(i);
}
function hide_show(i){
	//alert(22);
	$("#order_"+i).hover(function() {         
        $("#address_"+i).show();  
    },function(){
           $("#address_"+i).fadeOut(1000);  
        }

   ); 
}

function err_hide(){
	 $("#error").css("display","none"); 
}
</script>
</body>
</html>